

#include <stdio.h>
#include <cecore/cefile.h>
#include <cecore/cedir.h>
#include <cexml/cexml.h>

void WriteToFile(iFile* file)
{
	file->Write ("1234567890\n");
	file->Write ("1234567890\n");
}

void ReadFromFile(iFile* file)
{
	file->Seek (iFile::SP_End);
	long size = file->Tell ();
	file->Seek (iFile::SP_Set);

	char* buffer = new char [size+1];
	file->Read (buffer, size);
	buffer[size] = '\0';
	printf ("File Content:\n'%s'\nFileContentEnde\n", buffer);
	delete [] buffer;
}

void DebugElement (const ceXmlElement& element, unsigned indent)
{
	for (unsigned i=0; i<indent; i++) printf ("  ");
	if (!element.IsValid())
	{
		printf ("not valid\n");
		return;
	}

	printf ("<%s>%s\n", element.GetTagName().c_str (), element.GetString ().c_str());
	for (unsigned i=0, j=element.GetNumberOfChildren(); i<j; i++)
	{
		DebugElement (element.GetChild (i), indent+1);
	}
}

void OpenXml (const ceString& filename)
{
	ceXmlDocument doc;
	if (!doc.Open (filename))
	{
		printf ("unable to open document\n");
		return;
	}

	ceXmlElement root = doc.GetRootElement();
	DebugElement (root, 0);
	
}

void OpenXml (iFile* file)
{
	ceXmlDocument doc;
	if (!doc.Open (file))
	{
		printf ("unable to open document\n");
		return;
	}

	ceXmlElement root = doc.GetRootElement();
	DebugElement (root, 0);
	
}

int main (int argc, char** argv)
{
	ceDir dir;

	printf ("FullPath : %s\n", dir.GetCurrentPath().c_str());
	dir.GoUp();
	dir.GoUp();

	dir.ChangeDir ("bin");
	printf ("FullPath : %s\n", dir.GetCurrentPath().c_str());

	ceFile file(dir.GetCurrentPath() + "/test.txt");
	if (file.Open (iFile::OM_Write))
	{
		WriteToFile(&file);
		file.Close ();
	}

	if (file.Open (iFile::OM_Read))
	{
		ReadFromFile(&file);
		file.Close ();
	}

	ceFile xmlfile(dir.GetCurrentPath() + "/test.xml");
	if (xmlfile.Open (iFile::OM_Read))
	{
		OpenXml (&xmlfile);
	}


	printf ("Press \"Enter\" to exit\n");
	getchar();
  return 0;
}

